# -*-coding:utf-8-*-
from datetime import datetime, timedelta
from zhdate import from_datetime, year_tiandi, toalbnum, year_shengxiao
import sys
import getopt
import string
import time
# 朝代,帝号,年号查询不和日历整合
# 用年号对应农历，公历对应农历，即可用公历查年号，或用年号查公历

# 元组
mday = 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31


#注意python的/和//的区别
def whatweek(year, month, day):
    if month == 1 or month == 2:
        year = year - 1
        month = month + 12
    return (year + year//4 - year//100 + year//400 + (13*month+8)//5 + day) % 7


def is_leap(year):
    return year % 4 == 0 and year % 100 != 0 or year % 400 == 0


def monthdays(year, month):
    if month != 2:
        month = month - 1
        return mday[month]
    month = month - 1
    return mday[month] + is_leap(year)


def put_lunar(y, m, d):
    wd = whatweek(y, m, 1)
    mdays = monthdays(y, m)
    list1 = []
    list2 = []
    for i in range(1, mdays+1):
        list1.append([y, m, i])
    for i in range(0, mdays):
        list2.append(from_datetime(datetime(
            list1[i][0], list1[i][1], list1[i][2])))

    print("农历{}年{}月 岁在{} ({}年)".format(
        list2[mdays-1][0], list2[mdays-1][1], year_tiandi(toalbnum(list2[mdays-1][0])),year_shengxiao(toalbnum(list2[mdays-1][0]))))
    print("    日    一    二    三    四    五    六")
    print("------------------------------------------")
    i = 0
    while i < wd:
        print("      ", end="")
        i = i + 1
    for i in range(0, mdays):
        if list2[i][2] == "初一":
            if d == i+1:
                print("%s%4s%s" % ("\033[7m", list2[i][1], "\033[0m"), end="")
            else:
                print("%4s" % list2[i][1], end="")
            wd = wd + 1
            if wd % 7 == 0:
                print("")
        elif list2[i][2] != "初一":
            if d == i+1:
                print("%s%4s%s" % ("\033[7m", list2[i][2], "\033[0m"), end="")
            else:
                print("%4s" % list2[i][2], end="")
            wd = wd + 1
            if wd % 7 == 0:
                print("")
    if wd % 7 != 0:
        print("")
    return


def put_calendar(y, m, d):
    wd = whatweek(y, m, 1)
    mdays = monthdays(y, m)
    print(" 日 一 二 三 四 五 六")
    print("---------------------")
    i = 0
    while i < wd:
        print("   ", end="")
        i = i + 1
    for x in range(1, mdays+1):
        if x == d:
            print("{}{:3d}{}".format('\33[7m', x, '\033[0m'), end="")
        else:
            print("%3d" % x, end="")
        wd = wd + 1
        if wd % 7 == 0:
            print("")
    if wd % 7 != 0:
        print("")
    return


def main(argv):
    localtime = time.localtime(time.time())#一个；列表
    y = localtime[0]
    m = localtime[1]
    d = localtime[2]

    try:
        opts, argv = getopt.getopt(
            argv, "hy:m:d:l", ["year=", "month=", "day=", "lunar"])
    except getopt.GetoptError:
        print('calendar.py -y <year> -m <month>')
        sys.exit(2)
    for opt, arg in opts:
        if opt == '-h':
            print('calendar.py -y <year> -m <month>')
            sys.exit()
        elif opt in ("-y", "--year"):
            y = int(arg)
        elif opt in ("-m", "--month"):
            m = int(arg)
        elif opt in ("-d", "--day"):
            d = int(arg)
        elif opt in ("-l", "--lunar"):
            put_lunar(y, m, d)
            print("")

    print("公元{}年{}月".format(y, m))
    put_calendar(y, m, d)


if __name__ == '__main__':
    main(sys.argv[1:])
